# !/usr/bin/env python
# coding=utf-8
# @Time    : 2024/1/31 19:14
#
# stream_client.py


import requests

url = "http://127.0.0.1:8001/stream/"  # 替换为你的实际接口地址


def test1():
    try:
        response = requests.get(url, stream=True)  # stream参数为True

        if response.status_code == 200:
            for chunk in response.iter_content(chunk_size=6):  # 这行很重要哦  , decode_unicode=True
                if chunk:
                    print(chunk)
    except requests.RequestException as e:
        print(f"Request failed: {e}")


def test2():
    try:
        response = requests.get(url, stream=True)

        if response.status_code == 200:
            for line in response.iter_lines(decode_unicode=True, chunk_size=8):
                if line:
                    print("Received SSE event:", line)
    except requests.RequestException as e:
        print(f"Request failed: {e}")


test1()  # 能够逐字打印，这是因为response.iter_content是按块大小迭代的。
# test2()
